package eteam.aps.systemservice.service;

import cn.hutool.core.convert.Convert;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import eteam.aps.common.QueryResult;
import eteam.aps.common.R;
import eteam.aps.common.exceptions.AuthException;
import eteam.aps.systeminterface.model.dto.EmpDto;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import eteam.aps.systeminterface.model.entity.EmpEntity;
import eteam.aps.systeminterface.model.reqparam.IdParam;
import eteam.aps.systeminterface.model.reqparam.QueryEmpParam;
import eteam.aps.systeminterface.service.IEmpController;
import eteam.aps.systemservice.bll.EmpBll;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 职员服务
 */
@Api(tags = "职员服务控制器")
@RestController
//@RequestMapping(value = "/api/emp")
public class EmpController implements IEmpController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private EmpBll empBll;

    /**
     * 分页查询职员信息
     *
     * @param requestDto: 查询参数
     * @return
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "分页查询职员信息")
    @Override
    @GetMapping(value = "/api/emp/querypagelist")
    public R<QueryResult<EmpEntity>> queryEmpPageList(@Validated QueryEmpParam requestDto) throws Exception, AuthException {
        return R.success(empBll.queryEmpPageList(requestDto));
    }

    /**
     * 新增职员
     *
     * @param empDto: 职员信息
     * @return
     */
    @ApiOperationSupport(order = 2, ignoreParameters = {"empDto.fempid"})
    @ApiOperation(value = "新增职员")
    @Override
    @PostMapping(value = "/api/emp/createemp")
    public R createEmp(@Validated(Insert.class) @RequestBody EmpDto empDto) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        empBll.createEmp(tokenDto, empDto);
        return R.success();
    }

    /**
     * 更新职员
     *
     * @param empDto: 职员信息
     * @return
     */
    @ApiOperationSupport(order = 3, ignoreParameters = {})
    @ApiOperation(value = "更新职员")
    @Override
    @PostMapping(value = "/api/emp/updateemp")
    public R updateEmp(@Validated(Update.class) @RequestBody EmpDto empDto) throws Exception, AuthException {
        VerifyTokenDto tokenDto = Convert.convert(VerifyTokenDto.class, request.getAttribute("tokenInfo"));
        empBll.updateEmp(tokenDto, empDto);
        return R.success();
    }

    /**
     * 删除职员
     *
     * @param empParam: 职员Id参数
     * @return
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "删除职员")
    @Override
    @PostMapping(value = "/api/emp/deleteemp")
    public R deleteEmp(@Validated @RequestBody IdParam empParam) throws Exception, AuthException {
        empBll.deleteEmp(empParam.getObjid());
        return R.success();
    }
}
